import com.webobjects.foundation.*
import com.webobjects.eocontrol.*
import groovy.lang.webobjects.*

class NSArrayTest extends GroovyTestCase {
	static final LIST = [ "1", "2", "3", "3", "2", "1" ]
	static final NSLIST = new NSArray(LIST, true)

	void testCreation() {		
		NSArray a1 = LIST
		def a2 = LIST as NSArray
		def a3 = new NSArray(LIST)
		def a4 = new NSArray(LIST, true)

		def a5 = NSLIST as ArrayList
		def a6 = LIST as ArrayList
		
		assert a5 instanceof ArrayList	
		assertArrayEquals(a6.toArray(), a5.toArray())

		for (a in [ a1, a2, a3, a4 ]) {
			check(a)
			assertArrayEquals(LIST.toArray(), a.toArray())
		}
	}

	void testExample() {
    	def lang = [best: 'groovy', better: 'objective-c', good: 'java' ]
 	 	NSDictionary nslang = lang as NSMutableDictionary

 	 	assert nslang instanceof NSDictionary
 	 	assertArrayEquals(lang.collect{ it.value }.sort().toArray(), nslang.collect{ it.value }.sort().toArray())
    	 
     	lang['better'] = 'smalltalk';
  	 	nslang['better'] = 'smalltalk';
		ArrayList r1 = lang.collect{ it.value }.toArray()
		ArrayList r2 = nslang.collect{ it.value }.toArray()
		def r3 = nslang.collect{ it.value }
  	 	
 	 	assertArrayEquals(r1.sort().toArray(), r2.sort().toArray())
 	 	assert r3 instanceof NSArray
 	 	assertArrayEquals(r2.sort().toArray(), r3.sort().toArray())
 	 	
	 	NSMutableArray nslangList = nslang.collect { it -> 
	 		it.value.toString()[0].toUpperCase() + it.value.toString()[1..-1]; 
	 	} 
 	 	
 	 	def langList = lang.collect { it -> 
	 		it.value.toString()[0].toUpperCase() + it.value.toString()[1..-1]; 
	 	}
 	 	
 	 	assertArrayEquals(langList.sort().toArray(), nslangList.sort().toArray())
 	 	
	 	nslangList << 'perl' << 'python' << 'c' << 'ada' << 'prolog';
	 	langList << 'perl' << 'python' << 'c' << 'ada' << 'prolog';

 	 	assertArrayEquals(langList.sort().toArray(), nslangList.sort().toArray())
	}
	
	private check(array) {
		assert array instanceof NSArray
		assert array != null
	}

}
